package club.caihappy.service.impl;

import club.caihappy.constants.SystemConstants;
import club.caihappy.domain.ResponseResult;
import club.caihappy.domain.entity.Article;
import club.caihappy.domain.entity.Category;
import club.caihappy.domain.vo.ArticleDetailVO;
import club.caihappy.domain.vo.ArticleListVO;
import club.caihappy.domain.vo.HotArticleVO;
import club.caihappy.domain.vo.PageVO;
import club.caihappy.mapper.ArticleMapper;
import club.caihappy.service.ArticleService;
import club.caihappy.service.CategoryService;
import club.caihappy.utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by DELL on 2022/10/24
 **/
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private CategoryService categoryService;

    @Override
    public ResponseResult hotArticleList() {
        //查询热门文章，前十排名结果封装到ResponseResult返回
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper(Article.class);
        //必须是已发布文章不能是草稿
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        //按访问量排序
        queryWrapper.orderByDesc(Article::getViewCount);
        //分页选第一页
        Page<Article> page = new Page<>(SystemConstants.ARTICLE_PAGE_CURRENT, SystemConstants.ARTICLE_PAGE_SIZE);
        page(page, queryWrapper);
        List<Article> list = page.getRecords();
        List<HotArticleVO> hotArticleVOS = BeanCopyUtils.copyBeanList(list, HotArticleVO.class);
//        List<HotArticleVO> hotArticleVOS = ne w ArrayList<>();
//        for (Article article : list) {
////            HotArticleVO hotArticleVO = new HotArticleVO();
////            BeanUtils.copyProperties(article, hotArticleVO);
//            HotArticleVO hotArticleVO = BeanCopyUtils.copyBean(article, HotArticleVO.class);
//            hotArticleVOS.add(hotArticleVO);
//        }
        return ResponseResult.okResult(hotArticleVOS);
    }

    @Override
    public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId) {
        //查询条件
        LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //categoryid是否存在
        lambdaQueryWrapper.eq(Objects.nonNull(categoryId) && categoryId > 0, Article::getCategoryId, categoryId);
        //状态是正式发布的
        lambdaQueryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        //对isTop属性进行降序
        lambdaQueryWrapper.orderByDesc(Article::getIsTop, Article::getViewCount);
        //分页查询
        Page<Article> page = new Page<>(pageNum, pageSize);
        page(page, lambdaQueryWrapper);
        List<Article> articles = page.getRecords();
        //根据categoryId查categoryName
        //需要将article实体类添加@Accessors(chain = true)注解，目的是使setCategoryName()的返回值为article，这样就可以使用map
        articles.stream().map(a -> a.setCategoryName(categoryService.getById(a.getCategoryId()).getName())).collect(Collectors.toList());
        //bean拷贝到VO
        List<ArticleListVO> articleListVOS = BeanCopyUtils.copyBeanList(articles, ArticleListVO.class);
        PageVO pageVO = new PageVO(articleListVOS, page.getTotal());
        return ResponseResult.okResult(pageVO);
    }

    @Override
    public ResponseResult getArticleDetail(Long id) {
        //获取文章信息
        Article article = getById(id);
        //bean拷贝到vo
        ArticleDetailVO articleDetailVO = BeanCopyUtils.copyBean(article, ArticleDetailVO.class);
        //根据categoryId查categoryName
        Long categoryId = articleDetailVO.getCategoryId();
        Category category = categoryService.getById(categoryId);
        if (!Objects.isNull(category)) {
            articleDetailVO.setCategoryName(category.getName());
        }
        return ResponseResult.okResult(articleDetailVO);
    }
}
